package cn.school.controller.logistics;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.SysUserUtils;
import cn.school.logistics.entity.Assets;
import cn.school.logistics.entity.AssetsStoreDetils;
import cn.school.logistics.service.AssetsService;
import cn.school.logistics.service.AssetsStoreDetilsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "logistics-Assets-StoreDetils => 资产库存明细")
@RequestMapping("/logistics/Assets/storeDetils")
public class AssetsStoreDetilsController {
    @Autowired
    private AssetsStoreDetilsService teachingMaterialStoreDetilsService;
    @Autowired
    private AssetsService teachingMaterialService;


    @ApiOperation("获取列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<AssetsStoreDetils>> list(@RequestParam Map<String, Object> para) {
        int pageNum = Integer.parseInt((String) para.get("pageNum"));
        int pageSize = Integer.parseInt((String) para.get("pageSize"));
        String beginTime = (String) para.get("beginTime");
        String endTime = (String) para.get("endTime");
        String asseId = (String) para.get("asseId");

        // 分页条件
        Page<AssetsStoreDetils> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);

        // 搜索条件
        LambdaQueryWrapper<AssetsStoreDetils> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(asseId)) {
            queryWrapper.eq(AssetsStoreDetils::getAsseId, asseId);
        }
        if (StrUtil.isNotEmpty(beginTime)) {
            queryWrapper.ge(AssetsStoreDetils::getCreateTime, DateUtil.parse(beginTime + " 00:00:00", "yyyy-MM-dd HH:mm:ss"));
        }
        if (StrUtil.isNotEmpty(endTime)) {
            queryWrapper.le(AssetsStoreDetils::getCreateTime, DateUtil.parse(endTime + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }
        //租户
        queryWrapper.eq(AssetsStoreDetils::getTenantId, SysUserUtils.getSysUser().getTenantId());
        // 排序条件
        queryWrapper.orderByDesc(AssetsStoreDetils::getCreateTime);
        queryWrapper.orderByDesc(AssetsStoreDetils::getAsseId);

        IPage<AssetsStoreDetils> iPage = this.teachingMaterialStoreDetilsService.page(page, queryWrapper);
        if (iPage.getRecords() != null && iPage.getRecords().size() != 0) {
            List<Long> temaIdList = iPage.getRecords().stream().map(AssetsStoreDetils::getAsseId).collect(Collectors.toList());
            // 去重
            LinkedHashSet<Long> hashSet = new LinkedHashSet<>(temaIdList);
            temaIdList = new ArrayList<>(hashSet);
            // 获取对应教材的名称
            List<Assets> temaList = this.teachingMaterialService.listByIds(temaIdList);
            HashMap<Long, Assets> temaMap = (HashMap<Long, Assets>) temaList.stream().collect(Collectors.toMap(Assets::getId, obj -> obj));
            for (AssetsStoreDetils record : iPage.getRecords()) {
                if (temaMap.containsKey(record.getAsseId())) {
                    record.setAsseName(temaMap.get(record.getAsseId()).getAsseName());
                    record.setAsseCode(temaMap.get(record.getAsseId()).getAsseCode());
                }
            }
        }

        return CommonResult.success(CommonPage.restPage(iPage));
    }


    @ApiOperation("获取单个资产的入/出库的明细")
    @GetMapping("/info/{id}")
    public CommonResult<AssetsStoreDetils> getInfoById(@PathVariable Long id) {
        AssetsStoreDetils assetsStoreDetils = this.teachingMaterialStoreDetilsService.getById(id);
        return CommonResult.success(assetsStoreDetils);
    }


    @ApiOperation("添加入/出库记录")
    @PostMapping("/add")
    public CommonResult<AssetsStoreDetils> add(@RequestBody AssetsStoreDetils teachingMaterial) {
        LambdaQueryWrapper<AssetsStoreDetils> queryWrapper = new LambdaQueryWrapper<>();
        teachingMaterial.setCreateBy(SysUserUtils.getSysUser().getId());
        teachingMaterial.setCreateTime(Calendar.getInstance().getTime());
        teachingMaterial.setModifyTime(Calendar.getInstance().getTime());
        boolean isAddSuccess = this.teachingMaterialStoreDetilsService.saveBiz(teachingMaterial);
        if (isAddSuccess) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("资产下拉搜索条件")
    @PostMapping("/asseList")
    public CommonResult<List<Assets>> temaList(@RequestBody Assets teachingMaterial) {
        LambdaQueryWrapper<Assets> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Assets::getTenantId, SysUserUtils.getSysUser().getTenantId());
        if (StrUtil.isNotEmpty(teachingMaterial.getAsseName())) {
            queryWrapper.like(Assets::getAsseName, teachingMaterial.getAsseName()).or(
                    wrapper -> {
                        wrapper.like(Assets::getAsseCode, teachingMaterial.getAsseName());
                    }
            );
        }
        Page<Assets> page = new Page<>();
        page.setCurrent(1);
        page.setSize(100);
        page = this.teachingMaterialService.page(page, queryWrapper);
        return CommonResult.success(page.getRecords());
    }


}
